<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/24
 * Time: 14:12
 */

namespace app\admin\business;


use app\common\consts\BaseConst;
use app\common\helper\AdminInfoHelper;
use app\common\model\Product;

class ProductBusiness extends AdminBaseBusiness
{
    private $productModel;
    public function __construct(AdminInfoHelper $adminInfoHelper)
    {
        parent::__construct($adminInfoHelper);
        $this->productModel = new Product();
    }

    public function getList($name, $isHot, $isNew, $startTime, $endTime)
    {
        $map = [];
        if($name){
            $map[] = ['name','like','%'.$name.'%'];
        }
        if($isHot != -1){
            $map[] = ['isHot','=',$isHot];
        }
        if($isNew != -1){
            $map[] = ['isNew','=',$isNew];
        }
        if($startTime && $endTime){
            $startTime = date('Y-m-d 00:00:00', $startTime);
            $endTime = date('Y-m-d 23:59:59', $endTime);
            $map[] = ['created','between',[$startTime, $endTime]];
        }
        $list = $this->productModel
            ->where($map)
            ->order('sort desc,created desc,id desc')
            ->field($this->productModel->defaultFields)
            ->paginate(BaseConst::Admin_DEFAULT_PAGE_SIZE);
        $count = $this->productModel->where($map)->count();
        return [$list, $count];
    }

}